/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.tom.social.common.utils;

/**
 *
 * @author Tom
 */
public class URLTools {

	public static String clean(String url) {
		//TODO should be more complex then that
		if (!url.startsWith("http://") || url.length() < 8) {
			throw new IllegalArgumentException("URL '" + url + "' is not valide.");
		}
		
		//TODO this is bad. should be done elsewhere
		url = url.replace("http://www.", "http://");
		
		url = url.replace("?", "/");
		url = url.replace("%", "/");
		int linkEnd = url.substring(7).indexOf("/");
		if (linkEnd == -1) {
			if (url.length() < 10) {
				throw new IllegalArgumentException("URL is not valid.");
			} else {
				return url.endsWith("/") ? url : (url + "/");
			}
		}
		url = url.substring(0, linkEnd + 7);

		return url.endsWith("/") ? url : (url + "/");
	}

	public static boolean compareUrl(String url, String blogUrl) {
		String url1 = url.replace("http://www.", "http://");
		String url2 = blogUrl.replace("http://www.", "http://");
		if(url1.endsWith("/")) {
			return url1.equalsIgnoreCase(url2);
		}
		return (url1 + "/").equalsIgnoreCase(url2);
	}
}
